//切换查询模板
function changemodel(m, v) {
	if (m == 2) {
		var cell = document.getElementById("select" + v);
		cell.innerHTML = cell.innerHTML + "<input  type=\"text\" name=\"select"
				+ v + "\"> ";
		var c = document.getElementById("check" + v);
		c.selected = "selected";
		var x = document.getElementById("selectt" + v);
		x.value = 2
	}
	if (m == 1) {
		var cell = document.getElementById("select" + v);
		cell.innerHTML = cell.innerHTML.substring(0, cell.innerHTML
				.lastIndexOf('<'));
		var c = document.getElementById("check1" + v);
		c.selected = "selected";
		var x = document.getElementById("selectt" + v);
		x.value = 1
	}
}
var pg = 0;
// 条件分页取数据
function querydata(flag) {
	var pg = document.getElementById("pg");
	var pgpg = document.getElementById("pgpg");
	if (flag == 1) {
		pg.value = parseFloat(pg.value) + 1;
		pgpg.innerHTML = parseFloat(pg.value);
	}
	if (flag == 2) {
		pg.value = parseFloat(pg.value) - 1;

		if (pg.value <= 0) {
			pg.value = 1;
		}

		pgpg.innerHTML = parseFloat(pg.value);
	}
	if (flag == 3) {
		pg.value = 1;
		pgpg.innerHTML = 1;
	}
	// document.getElementById("selectquery").submit();
	var tb = document.getElementById("datatable");
	// var rowNum=tb.rows.length;
	// for (i=0;i<rowNum;i++)
	// {
	// tb.deleteRow(i);
	// rowNum=rowNum-1;
	// i=i-1;
	// }

	$("#datatable  tr:not(:first)").remove();

	$.ajax({
		cache : true,
		type : "POST",
		url : "queryAction!getDataByAjax",
		data : $('#selectquery').serialize(),
		async : false,
		error : function(request) {
			alert("Connection error");
		},
		success : function(data) {
			var datan = eval(data);
			var Table = document.getElementById("datatable");
			var i = 0;
			for ( var key in datan) {
				NewRow = Table.insertRow();
				for ( var ss in datan[key]) {
					for ( var sss in datan[key][ss]) {

						NewCell = NewRow.insertCell();
						NewCell.innerHTML = datan[key][ss][sss];
					}
				}
			}
		}
	});

}
// 废弃
function showornot() {
	// document.getElementById("1").style.visibility = "hidden";
	var tr = document.getElementById("1")
	tr.parentNode.removeChild(tr);
}

// 显示所选查询条件
function getselected(oid) {
	var tb = document.getElementById("NewTable");
	var rowNum = tb.rows.length;
	for (i = 0; i < rowNum; i++) {
		tb.deleteRow(i);
		rowNum = rowNum - 1;
		i = i - 1;
	}
	var checkselect = document.getElementsByName("checkselect");
	var objarray = checkselect.length;
	var str = "";
	for (i = 0; i < objarray; i++) {
		if (checkselect[i].checked == true) {
			str += checkselect[i].value + ",";
		}
	}
	if (str == "") {
		alert("请先选择一个条件～！");
	}
	// else
	// {
	// alert("您先择的是："+str);
	// }

	$
			.ajax({
				url : 'queryAction!getSelected',
				type : 'POST',// POST方式
				dataType : 'text',// 返回text类型
				data : {
					oid : oid
				},
				beforeSend : function(xmlHttpRequest, status) {
				},
				success : function(data, status) {
					var datan = eval(data);
					var Table = document.getElementById("NewTable");
					var i = 1;
					for ( var key in datan) {
						for (j = 0; j < objarray; j++) {
							if (checkselect[j].value == datan[key].id
									&& checkselect[j].checked == true) {
								NewRow = Table.insertRow();
								NewCell = NewRow.insertCell();
								if (datan[key].type == 1) {
									NewCell.id = "select" + i
									NewCell.innerHTML = datan[key].name
											+ ":<select name=\"select"
											+ i
											+ "\" style=\"max-width: 80px\" onchange=\"changemodel(this.value,"
											+ i
											+ ");\"> <option value=\"1\" id=\"check1"
											+ i
											+ "\">精确查询</option><option value=\"2\"  id=\"check"
											+ i
											+ "\"   >区间查询</option></select>  <input  type=\"hidden\" value=\""
											+ datan[key].name
											+ "\" name=\"select"
											+ i
											+ "\"> <input  type=\"hidden\" value=\""
											+ datan[key].type
											+ "\" name=\"select"
											+ i
											+ "\">  <input  type=\"hidden\" value=\""
											+ datan[key].model
											+ "\" name=\"select"
											+ i
											+ "\"> <input  type=\"text\" name=\"select"
											+ i
											+ "\">  <input  type=\"hidden\" value=\"1\" id=\"selectt"
											+ i + "\" name=\"selectt" + i
											+ "\"> ";
								}
								if (datan[key].type == 2) {
									NewCell.id = "select" + i
									NewCell.innerHTML = datan[key].name
											+ ":<select name=\"select"
											+ i
											+ "\" style=\"max-width: 80px\"><option>精确查询</option></select>  <input  type=\"hidden\" value=\""
											+ datan[key].name
											+ "\" name=\"select"
											+ i
											+ "\"> <input  type=\"hidden\" value=\""
											+ datan[key].type
											+ "\" name=\"select"
											+ i
											+ "\">  <input  type=\"hidden\" value=\""
											+ datan[key].model
											+ "\" name=\"select"
											+ i
											+ "\"> <input  type=\"text\" name=\"select"
											+ i
											+ "\">  <input  type=\"hidden\" value=\"1\" id=\"selectt"
											+ i + "\" name=\"selectt" + i
											+ "\">";
								}
								i++;
								break;
							}
						}
					}
				},
				complete : function(xmlHttpRequest, status) {

				},
				error : function() {
				}
			});
}

// 显示默认强制查询条件

function getselectedByDefult(oid, flag) {
	if (flag == 2) {
		var div = document.getElementById("namediv");
		div.style.visibility = "visible";
	}
	if (flag == 1) {
		var div = document.getElementById("namediv");
		div.style.visibility = "hidden";
	}

	var tb = document.getElementById("NewTable");
	var rowNum = tb.rows.length;
	for (i = 0; i < rowNum; i++) {
		tb.deleteRow(i);
		rowNum = rowNum - 1;
		i = i - 1;
	}
	$
			.ajax({
				url : 'queryAction!getSelectedDefult',
				type : 'POST',// POST方式
				dataType : 'text',// 返回text类型
				data : {
					oid : oid
				},
				beforeSend : function(xmlHttpRequest, status) {
				},
				success : function(data, status) {
					var datan = eval(data);
					var Table = document.getElementById("NewTable");
					var i = 1;
					for ( var key in datan) {
						NewRow = Table.insertRow();
						NewCell = NewRow.insertCell();
						if (datan[key].type == 1) {
							NewCell.id = "select" + i
							NewCell.innerHTML = datan[key].name
									+ ":<select name=\"select"
									+ i
									+ "\" style=\"max-width: 80px\" onchange=\"changemodel(this.value,"
									+ i
									+ ");\"> <option value=\"1\" id=\"check1"
									+ i
									+ "\" >精确查询</option><option value=\"2\"  id=\"check"
									+ i
									+ "\">区间查询</option></select>  <input  type=\"hidden\" value=\""
									+ datan[key].name
									+ "\" name=\"select"
									+ i
									+ "\"> <input  type=\"hidden\" value=\""
									+ datan[key].type
									+ "\" name=\"select"
									+ i
									+ "\">  <input  type=\"hidden\" value=\""
									+ datan[key].model
									+ "\" name=\"select"
									+ i
									+ "\"> <input  type=\"text\" name=\"select"
									+ i
									+ "\">  <input  type=\"hidden\" value=\"1\" id=\"selectt"
									+ i + "\" name=\"selectt" + i + "\"> ";
						}
						if (datan[key].type == 2) {
							NewCell.id = "select" + i
							NewCell.innerHTML = datan[key].name
									+ ":<select name=\"select"
									+ i
									+ "\" style=\"max-width: 80px\"><option>精确查询</option></select>  <input  type=\"hidden\" value=\""
									+ datan[key].name
									+ "\" name=\"select"
									+ i
									+ "\"> <input  type=\"hidden\" value=\""
									+ datan[key].type
									+ "\" name=\"select"
									+ i
									+ "\">  <input  type=\"hidden\" value=\""
									+ datan[key].model
									+ "\" name=\"select"
									+ i
									+ "\"> <input  type=\"text\" name=\"select"
									+ i
									+ "\">  <input  type=\"hidden\" value=\"1\" id=\"selectt"
									+ i + "\" name=\"selectt" + i + "\">";
						}
					}
				},
				complete : function(xmlHttpRequest, status) {

				},
				error : function() {
				}
			});
}